package rsj.sql.customer;

import com.rlyy.aibase.utils.base.MapNode;
import com.rlyy.aibase.utils.base.Node;
import com.rlyy.aibase.utils.base.TableNode;
import rsj.annotation.DynamicSelect;
import rsj.annotation.Param;
import rsj.annotation.Select;
import rsj.annotation.Update;
import rsj.api.customer.entity.Customer;
import rsj.service.customer.dynamic.CustomerDynamicStatement;

/**
 * 客户
 *
 * @author yuanqinglong
 * @since 2021/7/15 14:46
 */
public interface CustomerDao {


    /**
     * 得到客户
     *
     * @param customerId 客户id
     * @return {@link MapNode}
     */
    @Select("SELECT CONCAT('RSJ',LPAD(cust_id::text,4,'0')) AS cust_id,cust_legname,cust_sname,cust_name,cust_creditcode,cust_addr,cust_manoper,cust_manoperphone,cust_manopermail,cust_mantech," +
            " cust_mantechphone,cust_mantechmail,cust_manbus,cust_manbusphone,cust_manbusmail" +
            " FROM sys.sys_cust WHERE CONCAT('RSJ',LPAD(cust_id::text,4,'0')) = #{customerId}")
    MapNode getCustomer(@Param("customerId") String customerId);


    /**
     * 客户列表
     *
     * @param custSname 客户名称
     * @param custId    客户编号
     * @param pageSize  页面大小
     * @param pageNow   页面下标
     * @return {@link TableNode}
     */
    @DynamicSelect(type = CustomerDynamicStatement.class, method = "customerListDynamicSql")
    TableNode customerList(@Param("custSname") String custSname, @Param("custId") String custId,
                           @Param("pageSize") Integer pageSize, @Param("pageNow") Integer pageNow);


    /**
     * 客户列表统计
     *
     * @param custSname 客户名称
     * @param custId    客户编号
     * @param pageSize  页面大小
     * @param pageNow   页面下标
     * @return {@link TableNode}
     */
    @DynamicSelect(type = CustomerDynamicStatement.class, method = "customerListDynamicCountSql")
    Node customerCount(@Param("custSname") String custSname, @Param("custId") String custId,
                       @Param("pageSize") Integer pageSize, @Param("pageNow") Integer pageNow);


    /**
     * 更新客户
     *
     * @param customer 客户
     */
    @DynamicSelect(type = CustomerDynamicStatement.class, method = "dynamicUpdateCustomer")
    void updateCustomer(Customer customer);


    /**
     * 添加客户
     *
     * @param customer 客户
     */
    @Update("INSERT INTO sys.sys_cust (" +
            "cust_sname,cust_name,cust_creditcode,cust_addr,cust_manoper,cust_manoperphone,cust_manopermail,cust_mantech," +
            "cust_mantechphone,cust_mantechmail,cust_manbus,cust_manbusphone,cust_manbusmail,update_tm,cust_legname) " +
            " VALUES " +
            "(#{custSname},#{custName},#{custCreditcode},#{custAddr},#{custManoper},#{custManoperphone}" +
            ",#{custManopermail},#{custMantech},#{custMantechphone},#{custMantechmail},#{custManbus}," +
            "#{custManbusphone},#{custManbusmail},#{updateTm},#{custLegname})")
    void addCustomer(Customer customer);


    /**
     * 选择客户列表
     *
     * @param customerQueryParam 客户查询参数
     * @param custId             客户编号
     * @param pageSize           页面大小
     * @param pageNow            页面现在
     * @return {@link TableNode}
     */
    @DynamicSelect(type = CustomerDynamicStatement.class, method = "customerSelectListDynamicSql")
    TableNode selectCustomerList(@Param("customerName") String customerQueryParam, @Param("custId") String custId,
                                 @Param("pageSize") Integer pageSize, @Param("pageNow") Integer pageNow);


    /**
     * 通过id获取客户
     * 得到客户
     *
     * @param customerId 客户id
     * @return {@link MapNode}
     */
    @Select("SELECT CONCAT('RSJ',LPAD(cust_id::text,4,'0')) AS cust_id,cust_legname,cust_sname,cust_name,cust_creditcode,cust_addr,cust_manoper,cust_manoperphone,cust_manopermail,cust_mantech," +
            " cust_mantechphone,cust_mantechmail,cust_manbus,cust_manbusphone,cust_manbusmail" +
            " FROM sys.sys_cust WHERE cust_id = #{customerId}")
    MapNode getCustomerById(@Param("customerId") Integer customerId);


    /**
     * 通过账号标识查询客户简称
     *
     * @param autKey aut的关键
     * @return {@link Node}
     */
    @Select("SELECT cust_sname FROM sys.sys_cust LEFT JOIN sys.sys_org ON org_cust_id = cust_id LEFT JOIN sys.sys_authon ON org_id = aut_org_id " +
            " WHERE aut_keyid = #{autKey}")
    Node getCustSnameByAutKey(@Param("autKey") String autKey);
}
